/**
 * 
 */
package com.wsd.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wsd.bean.Articles;
import com.wsd.dao.ArticlesDaoInterface;
import com.wsd.util.PagingBuilder;

/**
 * @author TuZiJun
 *
 */
public class NewsAction extends ActionSupport {
	private ArticlesDaoInterface ArticlesDAO = null;
	private int aid;
	private int page = 1;
	private int model = 1;
	private static final int PAGESIZE = 10;
	public void setAid(int aid) {
		this.aid = aid;
	}
	/**
	 * @param model the model to set
	 */
	public void setModel(int model) {
		this.model = model;
	}
	/**
	 * @return the model
	 */
	public int getModel() {
		return model;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public void setArticlesDAO(ArticlesDaoInterface articlesDAO) {
		ArticlesDAO = articlesDAO;
	}
	public String newsDetail () {
		Articles article = ArticlesDAO.findById(aid);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("new", article);
		return SUCCESS;
	}
	public String showNews () {
		List<Articles> article = ArticlesDAO.findByModel((page - 1) * PAGESIZE, PAGESIZE , model);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("news", article);
		int max = ArticlesDAO.getTotalLines(model);
		max = max % PAGESIZE == 0 ? max / PAGESIZE : max / PAGESIZE + 1;
		int pagerange = max > 10 ? 10 : max;
		request.setAttribute("pageuri", PagingBuilder.build("/wsd/showNews.action?page=@page@&model=" + model, page, pagerange, max));
		return SUCCESS;
	}
}
